package flexo.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * Handles all the info messages of the engine.
 * @author Miguel
 */
public class Log
{	
	private String lastMsg;	
	private PrintWriter writer = null;
	
	/**
	 * Creates the log. It will print to the console if 'name' is null,
	 * otherwise it will create a file called name.htm where it will write
	 * the messages.
	 */
	public Log(String name)
	{
		lastMsg = new String();
		if (name != null)
		{
			try
			{
				writer = new PrintWriter(name + ".htm");
				writer.println("<html><head><title>"+name+"</title>");
				
				// Style definition
				writer.println("<style type=\"text/css\">");
				writer.println("ol {list-style-type: decimal-leading-zero;}");
				writer.println("li {margin-bottom: 2px; padding: 2px;}");
				writer.println(".standard {background-color:#C8FFD4; color:#005500;}");
				writer.println(".warning {background-color:#FAFF73; color:#555500;}");
				writer.println(".error {background-color:#FF6767; color:#550000;}");
				writer.println(".debug {background-color:#B3D3E9; color:#0000FF;}");
				writer.println("</style>");
				
				writer.println("</head><body>");
				writer.println("<h1>" + name + "</h1>");
				Date date = new Date();
				writer.println("<h3>Generated on " + date + "</h3>");
				writer.println("<p style=\"font-size: small;\"><em>Color code</em>: <span class=\"standard\">Standard</span> | <span class=\"warning\">Warning</span> | <span class=\"error\">Error</span> | <span class=\"debug\">Debug</span></p><hr/><ol>");
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Processes a message.
	 * @param origin The origin of the message, usually a class.
	 * @param msg the text of the message.
	 * @param type the type of message.
	 */
	public void addLog(String origin, String msg, LogMsgType type)
	{
		if (writer != null)
		{
			origin = "<strong>" + origin + "</strong>";
		}
		print(origin + "\t:\t" + msg, type);
	}
	
	protected void print(String str, LogMsgType type)
	{
		lastMsg = str;
		
		// Output to console
		if (writer == null)
		{
			java.lang.System.out.println(str);
		}
		// Output to file
		else
		{
			writer.print("<li");
			switch (type)
			{
			case STANDARD:
				writer.print(" class=\"standard\"");
				break;
			case WARNING:
				writer.print(" class=\"warning\"");
				break;
			case ERROR:
				writer.print(" class=\"error\"");
				break;
			case DEBUG:
				writer.print(" class=\"debug\"");
				break;
			}
			writer.println(">" + str + "</li>");			
		}
	}
	
	/**
	 * @return the last message added to the log.
	 */
	public String getLastMsg()
	{
		return lastMsg;
	}
	
	public void close()
	{
		// File cleanup
		if (writer != null)
		{
			writer.println("</ol></body></html>");
			writer.close();			
		}
	}
	
	@Override
	protected void finalize() throws Throwable
	{
		close();
	}
}
